package controllers;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.constants.RegistrationConstants;

import managerBeans.UserAccountManager;
import models.Address;
import models.StatusResponse;
import models.UserInfo;

/**
 * Servlet implementation class Register
 */
public class Register extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		UserInfo userInfo;
		PrintWriter out;

		out = response.getWriter();
		out = response.getWriter();
		out.println("<!DOCTYPE html>");
		out.println("<html>");
		out.println("<head>");
		out.println("<title>Form Data</title>");
		out.println("</body>");
		out.println("</html>");

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		UserInfo userInfo;
		PrintWriter out;

		userInfo = this.populateUserInfo(request);
		UserAccountManager manager = new UserAccountManager();
		StatusResponse statusResponse = manager.RegisterUser(userInfo);
		if (statusResponse.isSucess()) {
			
		} else {
			
		}
	}

	private UserInfo populateUserInfo(HttpServletRequest request) {

		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String emailAddress = request.getParameter("email");
		String userRole = RegistrationConstants.USER_ROLES.user.toString();
		String firstName = request.getParameter("firstname");
		String lastName = request.getParameter("firstname");
		String streetAddress = request.getParameter("address");
		String city = request.getParameter("city");
		String state = request.getParameter("state");
		String zipcode = request.getParameter("zipcode");
		String country = RegistrationConstants.DEFAULT_COUNTRY;

		Address address = new Address(streetAddress, city, state, zipcode,
				country);
		UserInfo userInfo = new UserInfo(username, password, userRole,
				firstName, lastName, emailAddress, address);

		return userInfo;
	}

}
